package cn.ac.big.bigd.util;

import cn.ac.big.bigd.model.submit.Submission;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 发动邮件
 * @author wangfan
 *
 */
public class EmailUtils {

    private static final Logger logger = LoggerFactory.getLogger(EmailUtils.class);


    public static boolean sendHtmlMail(String subject,String content,String receiver){

        boolean result = true;

        try{
            JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();  

            // 设定mail server  
            // 设定mail server  
            senderImpl.setHost(Constant.email_host);  
            senderImpl.setPort(Constant.email_port);
            senderImpl.setUsername(Constant.email);                             // 根据自己的情况,设置发件邮箱地址  
            senderImpl.setPassword(Constant.email_password);                    // 根据自己的情况, 设置password  
            senderImpl.setDefaultEncoding("UTF-8");
            Properties prop = new Properties();  
            prop.put("mail.smtp.auth", "true");                                 // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确  
            prop.put("mail.smtp.ssl.enable", "false");
            prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            senderImpl.setJavaMailProperties(prop);  

            // 建立邮件消息,发送简单邮件和html邮件的区别  
            MimeMessage mailMessage = senderImpl.createMimeMessage();  
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);  

            // 设置收件人，寄件人  
//            messageHelper.setTo(receiver);

            if(null != receiver && !receiver.isEmpty()){
                InternetAddress[] internetAddressTo = new InternetAddress().parse(receiver);
                messageHelper.setTo(internetAddressTo);
            }
            messageHelper.setFrom(Constant.email);  
            messageHelper.setSubject(subject);
            // true 表示启动HTML格式的邮件  
            messageHelper.setText(content,true);  

            // 发送邮件  
            senderImpl.send(mailMessage);  
        } catch (MessagingException e) {
            
            result = false;
            logger.info("EmailUtils.method [sendHtmlMail]: email send result-" + result +",error message-" + e);
        }

        return result;
    }




    public static boolean sendHtmlMailWithAttach(String subject,String content,String receiver,List<File> attachFileList){
        logger.info("send email 1....");
        boolean result = true;

        try{
            JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

            // 设定mail server
            // 设定mail server
            senderImpl.setHost(Constant.email_host);
            senderImpl.setPort(Constant.email_port);
            senderImpl.setUsername(Constant.email);                             // 根据自己的情况,设置发件邮箱地址
            senderImpl.setPassword(Constant.email_password);                    // 根据自己的情况, 设置password
            senderImpl.setDefaultEncoding("UTF-8");
            Properties prop = new Properties();
            prop.put("mail.smtp.auth", "true");                                 // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
            prop.put("mail.smtp.ssl.enable", "false");
            prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            senderImpl.setJavaMailProperties(prop);

            // 建立邮件消息,发送简单邮件和html邮件的区别
            MimeMessage mailMessage = senderImpl.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true);//如果想要带附件 加上true

            // 设置收件人，寄件人
//            messageHelper.setTo(receiver);

            if(null != receiver && !receiver.isEmpty()){
                InternetAddress[] internetAddressTo = new InternetAddress().parse(receiver);
                messageHelper.setTo(internetAddressTo);
            }
            messageHelper.setFrom(Constant.email);
            messageHelper.setSubject(subject);
            // true 表示启动HTML格式的邮件
            messageHelper.setText(content,true);

            for (File file : attachFileList) {
                logger.info("add file :" + file.getAbsolutePath());
                messageHelper.addAttachment(file.getName(),file);
            }



            // 发送邮件
            senderImpl.send(mailMessage);
        } catch (MessagingException e) {

            result = false;
            logger.info("EmailUtils.method [sendHtmlMail]: email send result-" + result +",error message-" + e);
        }

        return result;
    }

  public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("<div style=\"padding:24px 20px;font: bold 14px/22px arial,sans-serif;\">Dear " + 1 + " " + 2+ ","
                + "<br/>"
                + "<br/>We have received your WGS submission."
                + "<br/><table border=\"1\" ><tr> <th>Submission Title </th><th>Submission ID </th><th>BioProject</th><th>Biosample</th></tr>"
                + "<tr><td>"+2 + "</td><td>" + 3+ "</td><td>" + 4 + "</td><td>" +5+ "</td></tr></table>"
                + "<br/>After validation check processing, we have come across some issues which require your attention."
                + "<br/><br/>You need to check and be sure that all errors in the attached report files are fixed."
                + "<br/><br/>Please resubmit the corrected files at your convenience. To resubmit the corrected files, do not create a new submission. Instead, log back into genome submission portal, <a href='http://bigd.big.ac.cn/gwh/submit/submission'>http://bigd.big.ac.cn/gwh/submit/submission</a>. Revise the genome submission by clicking the corresponding submission ID, which is editable. Delete the files exist errors and upload replacement files, then click submit. Note that you need to reconfirm metadata from step1 to step6 when you revise your submission."
                +"<br/><br/>If you submit file by ftp, please log back into genome submission portal (<a href='http://bigd.big.ac.cn/gwh/submit/submission'>http://bigd.big.ac.cn/gwh/submit/submission</a>) after file submission, and click the \"Finish upload\" button in \"My Submissions\" list to inform us to check your submission."

                +"<br/><br/>Please send email to gwh@big.ac.cn if you have any question or revision about your submission!"
                +"<br/><br/>User:"+6
                + "<br/><br/>Sincerely,<br/>GWH team<br/><br/></div>");

        List<File> files =new ArrayList<>();
        files.add(new File("E:/360Cloud/BIGD/project/EHR/yjg_Inbody.xlsx"));
        files.add(new File("E:/360Cloud/BIGD/project/EHR/BIGD.pptx"));

        // 发送邮件
        EmailUtils.sendHtmlMailWithAttach("Feedback validation about", sb.toString(), "wangfan@big.ac.cn",files);


    }


}